//
//  ZRSYViewController.swift
//  XJ8XWB
//
//  Created by 中二 on 2016/10/23.
//  Copyright © 2016年 中二. All rights reserved.
//

import UIKit

// cell 可重用表示符
private let statusCellIdentifier = "statusCellIdentifier"


class ZRSYViewController: ZRUnloginViewController {


    
    // viewModel 对象
    let statusListViewModel = ZRStatusListViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if !isLogin {
            
            visitorView?.setupVisitorViewInfo(imgName: nil, tilte: nil)
            return
            
        }
        
        setupUI()
        getStatusData()
//        
//        getStatusData { (result) in
//            // 赋值
//            self.dataArray = result
//            // 刷新 UI
//            self.tableView.reloadData()
//        }
//        
//        print(navigationController?.childViewControllers.count)
        // Do any additional setup after loading the view.
    }

   private func setupUI(){
        
        setupNav()
    
     setupTableViewInfo()
    }
    
    
    // MARK: - 监听事件
    @objc private func leftClick(){
        print("左侧按钮点击")
    }
    
    @objc private func rightClick(){
        print("右侧按钮点击")
        // 创建
        let tempVc = ZRTempViewController()
        // 隐藏 tabbar
        tempVc.hidesBottomBarWhenPushed = true
        navigationController?.pushViewController(tempVc, animated: true)
        
//        print(navigationController?.childViewControllers.count)
        
    }
    
    private func setupNav(){
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(setHighlightedImgName: "navigationbar_pop", target: self, action: #selector(leftClick))
        navigationItem.rightBarButtonItem = UIBarButtonItem(setHighlightedImgName: "navigationbar_friendsearch", target: self, action: #selector(rightClick))
        
        
    }
    
    // MARK: - 设置 tableView 相关信息
    private func setupTableViewInfo(){
        
        // 设置数据源
        tableView.dataSource = self
        // 注册 cell
        tableView.register(ZRStatusCell.self, forCellReuseIdentifier: statusCellIdentifier)
        
        // cell 高度
//        tableView.rowHeight = 300
        // UITableViewAutomaticDimension 自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        // 设置预估行高estimatedRowHeight -> 越接近真实高度越好
        tableView.estimatedRowHeight = 200

    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
//// MARK: - 请求数据
//extension ZRSYViewController{
//    // 请求数据
//    func getStatusData(callback:@escaping ([ZRStatusModel])->()){
//        // 请求 url
//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        // 请求参数
//        let params = ["access_token": ZRUserAccountViewModel.sharedTools.accessToken!]
//        // 发送请求
//        ZRNetworkTools.sharedTools.requet(method: ZRNetworkToolsMethod.get, urlString: urlString, parameters: params, success: { (response) in
//           
//            // 判断response 是否为 nil 而且是否转成字典
//            guard let resDict = response as? [String: Any] else {
//                print("可能为 nil 可能转不出字典")
//                return
//            }
//            // 判断resDict["statuses"] 是否为 nil 而且是否可以转成数组
//            guard let resArr = resDict["statuses"] as? [[String: Any]] else {
//                print("可能为 nil 可能转不出数组")
//                return
//            }
//            // yymodel使用直接转出数组
//            let statusArray = NSArray.yy_modelArray(with: ZRStatusModel.self, json: resArr) as! [ZRStatusModel]
//            callback(statusArray)
//            
//        }) { (error) in
//            print("请求失败",error)
//        }
//    }
//}

// MARK: - 请求数据
extension ZRSYViewController{
    // 请求数据
    func getStatusData(){
        
        // 委托 statusListViewModel 请求数据
        statusListViewModel.getHomeData { (isSuccess) in
            if !isSuccess {
                print("请求数据失败")
                return
            }
            // 刷新 UI
            self.tableView.reloadData()
        }
    }
}
// MARK: - UITableViewDataSource
extension ZRSYViewController: UITableViewDataSource {
    
    
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.dataArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: statusCellIdentifier, for: indexPath) as! ZRStatusCell
        
        // 给 cell 赋值
        cell.statusViewModel = statusListViewModel.dataArray[indexPath.row]
        
        return cell
    }
}





